﻿using ECommon;
using ERefCommon;
using System;
using System.IO;

namespace EOrm.NpgsqlProvider
{
    /// <summary>
    /// 连接字符串的映射实体
    /// </summary>
    public class PgConStr : ConStr
    {
        /// <summary>
        /// 单例的实例
        /// </summary>
        private static PgConStr _conString = null;

        /// <summary>
        /// 单例锁
        /// </summary>
        private static readonly string _singltonLock = "_lockConString";

        /// <summary>
        /// 单例的创建
        /// </summary>
        /// <param name="jsonFilePathOrConnectionString">json配置文件路径或者连接字符串</param>
        /// <returns>单例实例</returns>
        public static PgConStr CreateInstance(string jsonFilePathOrConnectionString)
        {
            if (_conString == null)
            {
                lock (_singltonLock)
                {
                    if (_conString == null)
                    {
                        _conString = new PgConStr(jsonFilePathOrConnectionString);
                    }
                }
            }
            return _conString;
        }

        /// <summary>
        /// 单例的创建
        /// </summary>
        /// <returns>单例实例</returns>
        public static PgConStr CreateInstance()
        {
            if (_conString == null)
            {
                lock (_singltonLock)
                {
                    if (_conString == null)
                    {
                        _conString = new PgConStr();
                    }
                }
            }
            return _conString;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        private PgConStr()
        {
            Build("");
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="jsonFilePathOrConnectionString">json配置文件路径或链接字符串</param>
        private PgConStr(string jsonFilePathOrConnectionString)
        {
            Build(jsonFilePathOrConnectionString);
        }

        /// <summary>
        /// 构建连接字符串
        /// </summary>
        /// <param name="jsonFilePathOrConnectionString">json配置文件路径或链接字符串</param>
        private void Build(string jsonFilePathOrConnectionString)
        {
            string conStr = "";
            if (jsonFilePathOrConnectionString != null && !string.IsNullOrEmpty(jsonFilePathOrConnectionString.Trim()))
            {
                jsonFilePathOrConnectionString = AppDomain.CurrentDomain.BaseDirectory + "Config\\DbConfig.json";
            }
            if (File.Exists(jsonFilePathOrConnectionString))
            {
                DbHost = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbHost");
                DbPort = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbPort");
                DbUser = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbUser");
                DbPassword = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbPassword");
                DbName = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbName");
                DbEmbed = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbEmbed");
                DbInstance = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbInstance");
                DbTrusted = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbTrusted");
                DbPosition = JsonTool.GetJsonStr(jsonFilePathOrConnectionString, "DbPosition");
                conStr = string.Format("Server={0};Port={1};UserId={2};Password={3};Database={4};", DbHost, DbPort, DbUser, DbPassword, DbName);
            }
            else
            {
                conStr = jsonFilePathOrConnectionString;
            }
            SetConStr(conStr);
        }

        /*
         * switch (DbStyle)
                {
                    case "sqlserver":
                        return DbTrusted.ToLower() == "true" ? string.Format(@"Data source={0}\{1}; Initial Catalog={2};Integrated Security=true;", DbHost, DbInstance, DbName) : string.Format(@"Data source={0}\{1}; Initial Catalog={2}; User Id={3}; Password={4};", DbHost, DbInstance, DbName, DbUser, DbPassword);
                    case "sqlite":
                        DbPosition = DbPosition == null || string.IsNullOrEmpty(DbPosition.Trim()) ? string.Format("{0}data.db", AppDomain.CurrentDomain.BaseDirectory) : DbPosition;
                        DbPosition = DbPosition.Replace('\\', Path.DirectorySeparatorChar);
                        if (!File.Exists(DbPosition))
                        {
                            throw new ArgumentException("sqlite file not exist", "DbPosition");
                        }
                        return string.Format("Data Source={0};version=3;", DbPosition);
                    case "postgres":
                        return string.Format("Server={0};Port={1};UserId={2};Password={3};Database={4};", DbHost, DbPort, DbUser, DbPassword, DbName);
                    default:
                        throw new Exception(string.Format("not support database: {0}", DbStyle));
                }
         */

    }
}
